package com.project.service.impl;


import com.project.bean.CutPageBean;
import com.project.bean.ShopBean;
import com.project.bean.UserBean;
import com.project.mapper.IShopMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IShopService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class ShopServiceImpl extends  BaseService implements IShopService {

    @Override
    public int add(ShopBean shop) {
        SqlSession session=this.getSession();
        IShopMapper mapper=session.getMapper(IShopMapper.class);
        mapper.add(shop);
        session.commit();
        session.close();
        return shop.getId();
    }

    @Override
    public void del(int id) {
        //得到会话
        SqlSession session=this.getSession();
        //得到指定mapper接口的实现类对象
        IShopMapper mapper=session.getMapper(IShopMapper.class);
        //完成添加
        mapper.del(id);;
        //提交事务
        session.commit();

        //关闭
        session.close();


    }

    @Override
    public void update(int id, int price) {
        //得到会话
        SqlSession session=this.getSession();
        //得到指定mapper接口的实现类对象
        IShopMapper mapper=session.getMapper(IShopMapper.class);
        //完成添加
        mapper.update(id,price);;
        //提交事务
        session.commit();

        //关闭
        session.close();


    }

    @Override
    public List<ShopBean> findByBirthday(LocalDate startDate, LocalDate endDate) {
        SqlSession session= this.getSession();
        IShopMapper mapper=session.getMapper(IShopMapper.class);
        List<ShopBean>list=mapper.findByBirthday(startDate,endDate);
        session.close();
        return list;

    }

    @Override
    public void addMore(List<ShopBean> list) {
        SqlSession session=this.getSession();
        IShopMapper mapper=session.getMapper(IShopMapper.class);
        mapper.addMore(list);
        session.commit();
        session.close();

    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session=this.getSession();
        IShopMapper mapper=session.getMapper(IShopMapper.class);
        mapper.delMore(idArray);

        session.commit();
        session.close();

    }

    @Override
    public CutPageBean<ShopBean> cutAll(int pageNO, String name, LocalDate startDate, LocalDate endDate) {

        CutPageBean<ShopBean>cunBean=new CutPageBean<>();
        SqlSession session=this.getSession();
        IShopMapper mapper= session.getMapper(IShopMapper.class);
        //得到当前页需要显示的数据

        List<ShopBean>list=mapper.cutAllList((pageNO-1)*PAGESIZE,PAGESIZE,name,startDate,endDate);
        //得到总记录数
        int count=mapper.cutAllCount();
        //得到总页数
        int totalPage=count%PAGESIZE==0?
                count/PAGESIZE:count/PAGESIZE+1;
        //将数据封装为分页对象
        cunBean.setList(list);
        cunBean.setCount(count);
        cunBean.setTitlePage(totalPage);
        session.close();
        return cunBean;

    }
}
